﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_Petitions : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Petitions pet = new B_FIM_Petitions();
        IBLL_FIM_PetitionDtls petd = new B_FIM_PetitionDtls();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_BGM_MAS_ExpenseGroup expgrp = new B_BGM_MAS_ExpenseGroup();
        IBLL_HRM_PS_Employees emplist = new B_HRM_PS_Employees();
        B_MessageSend send = new B_MessageSend();
        IBLL_CodeGenerate cg = new B_CodeGenerate();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                lblBgmYear.Text = year.ToString();

                UpdateUI1("ViewAll");
                LoadDDLFilterBgmSourceSeq();
                ddlFilterBgmSourceSeq.SelectedIndex = 0;
                LoadRblBgmExpGrpSeq();
                lblBgmSourceName.Text = ddlFilterBgmSourceSeq.SelectedItem.Text;
                dpPetitionDate.SetValue(DateTime.Today);
            }
        }

        protected void LoadRadGrid1()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            RadGrid1.DataSource = pet.GetFull(strBgmSourceSeq, strBgmYear);
        }

        protected void LoadRadGrid2()
        {
            LoadRadGrid2(null);
        }

        protected void LoadRadGrid2(string strPetitionSeq)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;

            RadGrid2.DataSource = mr.GetNotPetition(strBgmSourceSeq, strBgmYear, strPetitionSeq, strBgmExpGrpSeq);
            RadGrid2.DataBind();
        }

        protected void CheckPetitionRequest(string strPetitionSeq)
        {
            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                if (RadGrid2.Items[i]["petition_seq"].Text == strPetitionSeq)
                {
                    RadGrid2.Items[i].Selected = true;
                }
            }
        }

        protected void LoadDDLFilterBgmSourceSeq()
        {
            ddlFilterBgmSourceSeq.DataSource = source.GetFull(true);
            ddlFilterBgmSourceSeq.DataBind();
        }

        protected void LoadRblBgmExpGrpSeq()
        {
            rblBgmExpGrpSeq.DataSource = expgrp.GetList(null);
            rblBgmExpGrpSeq.DataBind();

            rblBgmExpGrpSeq.SelectedIndex = 1;
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;
                    cmdApprove.Visible = false;
                    cmdPetitionNo.Visible = true;
                    cmdReceive.Visible = true;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    lblMode.Text = "แสดงข้อมูล";
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;
                    cmdApprove.Visible = false;
                    cmdPetitionNo.Visible = false;
                    cmdReceive.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    lblMode.Text = "เพิ่มรายการ";
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;
                    cmdApprove.Visible = true;
                    cmdPetitionNo.Visible = false;
                    cmdReceive.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    lblMode.Text = "แก้ไขรายการ";
                    break;
            }
        }

        protected void ClearControl1()
        {
            lblPetitionSeq.Text = "";
            lblAmountCollateral.Text = "-0.00";
            lblAmountFund.Text = "-0.00";
            lblAmountPenalty.Text = "-0.00";
            lblAmountTax.Text = "-0.00";
            lblAmountReal.Text = "0.00";
            lblBathAmount.Text = "0.00";
            lblRefNo.Text = "";
            lblPetitionNo.Text = "";
            lblReceiveDate.Text = "";
            dpPetitionDate.SetValue(DateTime.Today);
        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                string strPetitionNo = main.WebString(item["petition_no"].Text);
                string strRecordStatus = main.WebString(item["record_status"].Text);

                cmdPetitionNo.Enabled = strPetitionNo.Length == 0;
                cmdReceive.Enabled = strRecordStatus == "A";
                cmdDelete1.Enabled = strRecordStatus != "A";
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")
            {
                if (RadGrid1.SelectedItems.Count > 0)
                {
                    string strPetitionSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_seq"].Text;
                    LoadRadGrid2(main.WebString(strPetitionSeq));
                }
                else
                {
                    LoadRadGrid2(null);
                }
            }
            else if (e.CommandName == "RowClick" || e.CommandName == "CheckAll")
            {
                double dSum = 0;
                double dSumTax = 0;
                double dSumPenalty = 0;
                double dSumCollateral = 0;
                double dSumFund = 0;
                double dSumReal = 0;
                string strAmount = "0.00";
                string strAmountTax = "-0.00";
                string strAmountPenalty = "-0.00";
                string strAmountRedemption = "-0.00";
                string strAmountFund = "-0.00";
                string strAmountReal = "-0.00";

                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    if (RadGrid2.Items[i].Selected)
                    {
                        strAmount = main.WebString(RadGrid2.Items[i]["total_bath_amount"].Text);
                        strAmount = (!string.IsNullOrEmpty(strAmount) ? strAmount : "0.00");
                        dSum = dSum + Convert.ToDouble(strAmount);

                        strAmountTax = main.WebString(RadGrid2.Items[i]["total_amount_tax"].Text);
                        strAmountTax = (!string.IsNullOrEmpty(strAmountTax) ? strAmountTax : "0.00");
                        dSumTax = dSumTax + Convert.ToDouble(strAmountTax);

                        strAmountPenalty = main.WebString(RadGrid2.Items[i]["total_amount_penalty"].Text);
                        strAmountPenalty = (!string.IsNullOrEmpty(strAmountPenalty) ? strAmountPenalty : "0.00");
                        dSumPenalty = dSumPenalty + Convert.ToDouble(strAmountPenalty);

                        strAmountRedemption = main.WebString(RadGrid2.Items[i]["total_amount_redemption"].Text);
                        strAmountRedemption = (!string.IsNullOrEmpty(strAmountRedemption) ? strAmountRedemption : "0.00");
                        dSumCollateral = dSumCollateral + Convert.ToDouble(strAmountRedemption);

                        strAmountFund = main.WebString(RadGrid2.Items[i]["total_amount_fund"].Text);
                        strAmountFund = (!string.IsNullOrEmpty(strAmountFund) ? strAmountFund : "0.00");
                        dSumFund = dSumReal + Convert.ToDouble(strAmountFund);

                        strAmountReal = main.WebString(RadGrid2.Items[i]["total_amount_real"].Text);
                        strAmountReal = (!string.IsNullOrEmpty(strAmountReal) ? strAmountReal : "0.00");
                        dSumReal = dSumReal + Convert.ToDouble(strAmountReal);
                    }
                }

                lblBathAmount.Text = dSum.ToString("#,##0.00");
                lblAmountTax.Text = dSumTax.ToString("-#,##0.00");
                lblAmountPenalty.Text = dSumPenalty.ToString("-#,##0.00");
                lblAmountCollateral.Text = dSumCollateral.ToString("-#,##0.00");
                lblAmountFund.Text = dSumFund.ToString("-#,##0.00");
                lblAmountReal.Text = dSumReal.ToString("#,##0.00");
            }
        }

        protected void ddlFilterBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRblBgmExpGrpSeq();
            LoadRadGrid1();
            RadGrid1.DataBind();
            lblBgmSourceName.Text = ddlFilterBgmSourceSeq.SelectedItem.Text;
        }

        protected void ddlFilterWorkAreaSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
            lblBgmYear.Text = txtBgmYear.Text;
        }

        protected bool CheckData1()
        {
            bool bOK = true;
            string strError = "";

            if (dpPetitionDate.GetStringValue().Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("วันที่ทำรายการ");
            }
            else if (RadGrid2.SelectedItems.Count == 0)
            {
                bOK = false;
                strError = "กรุณาเลือกรายการรอเบิก";
            }

            if (!bOK)
                main.MessageAlert(Page, strError);

            return bOK;
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
            rblBgmExpGrpSeq.Enabled = true;
            rblPetitionType.Enabled = true;

            LoadRadGrid2(null);
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strPetitionSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_seq"].Text;
                string strRefNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ref_no"].Text;
                string strPetitionDate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_date"].Text;
                string strPetitionNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_no"].Text;
                string strDirectPay = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["direct_pay"].Text;
                string strAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["sum_amount"].Text;
                string strAmountTax = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["sum_amount_tax"].Text;
                string strAmountPenalty = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["sum_amount_penalty"].Text;
                string strAmountRedemption = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["sum_amount_redemption"].Text;
                string strAmountFund = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["sum_amount_fund"].Text;
                string strAmountReal = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["sum_amount_real"].Text;
                string strBgmExpGrpSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bgm_exp_grp_seq"].Text;
                string strRecordStatus = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["record_status"].Text;

                lblPetitionSeq.Text = main.WebString(strPetitionSeq);
                lblRefNo.Text = main.WebString(strRefNo);
                dpPetitionDate.SetValue(main.WebString(strPetitionDate));
                lblPetitionNo.Text = main.WebString(strPetitionNo);
                rblDirectPay.SelectedValue = main.WebString(strDirectPay);
                lblBathAmount.Text = main.WebString(strAmount);
                lblAmountTax.Text = "-" + main.WebString(strAmountTax);
                lblAmountPenalty.Text = "-" + main.WebString(strAmountPenalty);
                lblAmountCollateral.Text = "-" + main.WebString(strAmountRedemption);
                lblAmountFund.Text = "-" + main.WebString(strAmountFund);
                lblAmountReal.Text = main.WebString(strAmountReal);
                rblBgmExpGrpSeq.SelectedValue = main.WebString(strBgmExpGrpSeq);
                rblBgmExpGrpSeq.Enabled = false;
                rblPetitionType.Enabled = false;
                cmdUpdate1.Visible = strRecordStatus == "U" || strRecordStatus == "W" || strRecordStatus == "R";
                cmdApprove.Visible = strRecordStatus == "U" || strRecordStatus == "W" || strRecordStatus == "R";

                LoadRadGrid2(strPetitionSeq);
                CheckPetitionRequest(strPetitionSeq);
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                if (RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["record_status"].Text == "A")
                {
                    main.MessageAlert(Page, "ไม่สามารถลบรายการนี้ได้ เนื่องจากได้รับอนุมัติแล้ว");
                    return;
                }

                M_FIM_Petitions info = new M_FIM_Petitions();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strPetitionSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_seq"].Text;

                info.petition_seq = Convert.ToInt32(strPetitionSeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = main.ConvertNullDateTime(dtNow);

                pet.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdApprove_Click(object sender, EventArgs e)
        {
            PanelMessageSend.Visible = true;
            ddlApprover.SelectedIndex = -1;
            lblAppClassName.Text = "";
            lblAppExecPosName.Text = "";
            lblAppJobsiteName.Text = "";
            lblAppPosName.Text = "";
            lblEmpSeq.Text = "";
        }

        protected void ddlApprover_DataBound(object sender, EventArgs e)
        {
            //set the initial footer label
            ((Literal)ddlApprover.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(ddlApprover.Items.Count);
        }

        protected void ddlApprover_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            ddlApprover.DataSource = emplist.GetFull("1", null, null);
            ddlApprover.DataBind();
        }

        protected void ddlApprover_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = ((M_HRM_PS_Employees)e.Item.DataItem).emp_name;
            e.Item.Value = ((M_HRM_PS_Employees)e.Item.DataItem).emp_seq.ToString();
        }

        protected void ddlApprover_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            SetEmpInfo(ddlApprover.SelectedValue);
        }

        protected void SetEmpInfo(string strEmpSeq)
        {
            if (strEmpSeq.Length > 0)
            {
                M_HRM_PS_Employees info = emplist.GetInfo(strEmpSeq);

                lblEmpSeq.Text = info.emp_seq.ToString();
                ddlApprover.Text = info.emp_name;
                lblAppPosName.Text = info.pos_name;
                lblAppClassName.Text = info.class_name;
                lblAppExecPosName.Text = info.exec_pos_name;
                lblAppJobsiteName.Text = info.working_job_site_name;
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (lblEmpSeq.Text.Length == 0)
            {
                ((RadAjaxManager)Master.FindControl("RadAjaxManager1")).Alert("คำเตือน!! กรุณาเลือกผู้อนุมัติก่อนส่ง");
                //RadAjaxManager1.Alert("คำเตือน!! กรุณาเลือกผู้อนุมัติก่อนส่ง");
                return;
            }

            try
            {
                string strPetitionSeq = lblPetitionSeq.Text;
                M_MessageSend info = new M_MessageSend();
                info.send_date = DateTime.Today;
                info.ref_seq = main.ConvertNullInt(strPetitionSeq);
                info.ref_type = "fim_petitions";
                info.ref_no = lblRefNo.Text;
                info.send_text = "ส่งขออนุมัติเบิกเงินในระบบ GFMIS " + lblRefNo.Text;
                info.from_emp_seq = main.ConvertNullInt(Session["EmpSeq"]);
                info.from_pos_seq = send.GetPosSeqFromEmpSeq("" + Session["EmpSeq"]);
                info.to_emp_seq = main.ConvertNullInt(lblEmpSeq.Text);
                info.to_pos_seq = send.GetPosSeqFromEmpSeq(lblEmpSeq.Text);
                info.from_send_seq = null;
                info.remarks = null;
                info.message_status = "0";
                info.created_by = main.ConvertNullInt(Session["EmpSeq"]);
                info.created_when = DateTime.Now;
                send.Insert(info);

                M_FIM_Petitions info2 = new M_FIM_Petitions();
                info2.petition_seq = Convert.ToInt32(strPetitionSeq);
                info2.record_status = "P";
                info2.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
                info2.updated_when = DateTime.Now;
                pet.ChangeStatus(info2);
                cmdApprove.Enabled = false;
                main.MessageAlert(Page, "บันทึกเรียบร้อยแล้ว");

                PanelMessageSend.Visible = false;
                LoadRadGrid1();
                RadGrid1.DataBind();
                UpdateUI1("Insert");
                ClearControl1();
            }
            catch
            {
                main.MessageAlert(Page, "เกิดข้อผิดพลาดในการบันทึกข้อมูล");
            }
        }

        protected void cmdPetitionNo_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                string strPetitionSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_seq"].Text;
                Session["PetitionSeq"] = strPetitionSeq;

                ScriptManager.RegisterClientScriptBlock(
                    this.Page,
                    this.GetType(),
                    "WebUserControlSript",
                    "OpenUpdatePetitionNo()",
                    true);
            }
        }

        protected void cmdReceive_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                string strPetitionSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_seq"].Text;
                Session["PetitionSeq"] = strPetitionSeq;

                ScriptManager.RegisterClientScriptBlock(
                    this.Page,
                    this.GetType(),
                    "WebUserControlSript",
                    "OpenUpdatePetitionReceive()",
                    true);
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            if (!CheckData1())
            {
                return;
            }

            M_FIM_Petitions info = new M_FIM_Petitions();
            
            string strPetitionSeq = lblPetitionSeq.Text;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strRefNo = cg.GetNextCode(TransectionType.FimPetitionRefNo, strBgmSourceSeq == "1" ? "1" : "2");
            string strPetitionDate = dpPetitionDate.GetStringValue();
            string strDirectPay = rblDirectPay.SelectedValue;
            string strPetitionType = rblPetitionType.SelectedValue;
            //string strRemarks = txtRemarks.Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            string strMoneyReqSeq;
            string strJobsiteSeq;
            string strAmount;
            string strAmountTax;
            string strAmountPenalty;
            string strAmountRedemption;
            string strAmountFund;
            string strAmountReal;

            //info.transfer_seq = Convert.ToInt32(strTransferSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.ref_no = strRefNo;
            info.petition_date = main.ConvertNullDateTime(strPetitionDate);
            info.petition_type = strPetitionType;
            info.direct_pay = strDirectPay;
            //info.remarks = strRemarks;

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            strPetitionSeq = pet.Insert(info);

            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                if (RadGrid2.Items[i].Selected)
                {
                    M_FIM_PetitionDtls dtlsInfo = new M_FIM_PetitionDtls();
                    strMoneyReqSeq = main.WebString(RadGrid2.Items[i]["money_req_seq"].Text);
                    strAmount = main.WebString(RadGrid2.Items[i]["total_amount"].Text);
                    strAmountTax = main.WebString(RadGrid2.Items[i]["total_amount_tax"].Text);
                    strAmountPenalty = main.WebString(RadGrid2.Items[i]["total_amount_penalty"].Text);
                    strAmountRedemption = main.WebString(RadGrid2.Items[i]["total_amount_redemption"].Text);
                    strAmountFund = main.WebString(RadGrid2.Items[i]["total_amount_fund"].Text);
                    strAmountReal = main.WebString(RadGrid2.Items[i]["total_amount_real"].Text);
                    strJobsiteSeq = main.WebString(RadGrid2.Items[i]["job_site_seq"].Text);

                    dtlsInfo.petition_seq = main.ConvertNullInt(strPetitionSeq);
                    dtlsInfo.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
                    dtlsInfo.job_site_seq = main.ConvertNullInt(strJobsiteSeq);
                    dtlsInfo.amount = main.ConvertNullDouble(strAmount);
                    dtlsInfo.amount_tax = main.ConvertNullDouble(strAmountTax);
                    dtlsInfo.amount_penalty = main.ConvertNullDouble(strAmountPenalty);
                    dtlsInfo.amount_redemption = main.ConvertNullDouble(strAmountRedemption);
                    dtlsInfo.amount_fund = main.ConvertNullDouble(strAmountFund);
                    dtlsInfo.amount_real = main.ConvertNullDouble(strAmountReal);
                    dtlsInfo.created_by = main.ConvertNullInt(strUserID);
                    dtlsInfo.created_when = dtNow;

                    petd.Insert(dtlsInfo);
                }
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            if (!CheckData1())
            {
                return;
            }

            M_FIM_Petitions info = new M_FIM_Petitions();

            string strPetitionSeq = lblPetitionSeq.Text;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strRefNo = lblRefNo.Text;
            string strPetitionDate = dpPetitionDate.GetStringValue();
            string strDirectPay = rblDirectPay.SelectedValue;
            string strPetitionType = rblPetitionType.SelectedValue;
            //string strRemarks = txtRemarks.Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            string strMoneyReqSeq;
            string strJobsiteSeq;
            string strReqPetitionSeq;
            string strReqPetitionDtlsSeq;
            string strAmount;
            string strAmountTax;
            string strAmountPenalty;
            string strAmountRedemption;
            string strAmountFund;
            string strAmountReal;

            info.petition_seq = Convert.ToInt32(strPetitionSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.ref_no = strRefNo;
            info.petition_date = main.ConvertNullDateTime(strPetitionDate);
            info.petition_type = strPetitionType;
            info.direct_pay = strDirectPay;
            //info.remarks = strRemarks;

            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            pet.Update(info);

            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                M_FIM_PetitionDtls dtlsInfo = new M_FIM_PetitionDtls();
                strMoneyReqSeq = main.WebString(RadGrid2.Items[i]["money_req_seq"].Text);
                strAmount = main.WebString(RadGrid2.Items[i]["total_amount"].Text);
                strAmountTax = main.WebString(RadGrid2.Items[i]["total_amount_tax"].Text);
                strAmountPenalty = main.WebString(RadGrid2.Items[i]["total_amount_penalty"].Text);
                strAmountRedemption = main.WebString(RadGrid2.Items[i]["total_amount_redemption"].Text);
                strAmountFund = main.WebString(RadGrid2.Items[i]["total_amount_fund"].Text);
                strAmountReal = main.WebString(RadGrid2.Items[i]["total_amount_real"].Text);
                strJobsiteSeq = main.WebString(RadGrid2.Items[i]["job_site_seq"].Text);
                strReqPetitionSeq = main.WebString(RadGrid2.Items[i]["petition_seq"].Text);
                strReqPetitionDtlsSeq = main.WebString(RadGrid2.Items[i]["petition_dtls_seq"].Text);

                dtlsInfo.petition_seq = main.ConvertNullInt(strPetitionSeq);
                dtlsInfo.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
                dtlsInfo.job_site_seq = main.ConvertNullInt(strJobsiteSeq);
                dtlsInfo.amount = main.ConvertNullDouble(strAmount);
                dtlsInfo.amount_tax = main.ConvertNullDouble(strAmountTax);
                dtlsInfo.amount_penalty = main.ConvertNullDouble(strAmountPenalty);
                dtlsInfo.amount_redemption = main.ConvertNullDouble(strAmountRedemption);
                dtlsInfo.amount_fund = main.ConvertNullDouble(strAmountFund);
                dtlsInfo.amount_real = main.ConvertNullDouble(strAmountReal);
                dtlsInfo.created_by = main.ConvertNullInt(strUserID);
                dtlsInfo.created_when = main.ConvertNullDateTime(dtNow);
                dtlsInfo.updated_by = main.ConvertNullInt(strUserID);
                dtlsInfo.updated_when = main.ConvertNullDateTime(dtNow);

                if (RadGrid2.Items[i].Selected)
                {
                    if (strReqPetitionSeq.Length == 0)
                    {
                        dtlsInfo.petition_seq = Convert.ToInt32(strPetitionSeq);

                        petd.Insert(dtlsInfo);
                    }
                }
                else
                {
                    dtlsInfo.petition_dtls_seq = Convert.ToInt32(strReqPetitionDtlsSeq);

                    if (strReqPetitionSeq.Length == 0)
                    {
                        petd.Update(dtlsInfo);
                    }
                    else
                    {
                        petd.Delete(dtlsInfo);
                    }
                }
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        protected void rblBgmExpGrpSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmdInsert1.Visible)
            {
                LoadRadGrid2();
            }
            else
            {
                string strPetitionSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["petition_seq"].Text;
                LoadRadGrid2(main.WebString(strPetitionSeq));
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        //==========================================================================
    }
}
